package leetcode.easy.q144;

import leetcode.util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */

/**
 * 中序遍历=中根遍历。对应还有先根遍历，后根遍历
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        visit(root, res);
        return res;

    }

    private void visit(TreeNode root, List<Integer> res) {
        res.add(root.val);
        if (root.left != null) visit(root.left, res);
        if (root.right != null) visit(root.right, res);
    }


}